<?php

/**
 * Description of Notification
 *
 * @author Administrator
 */
class Notification extends CI_Controller {

  protected $class;
  protected $model;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    $model_name = $this->class."_model";
    $this->model = new $model_name();
    set_userdata('currentURL', current_url());
  }

  protected function _list_data() {
    return array();
  }

  public function index($max = '', $offset = '') {
    $offset = $offset + 0;
    $max = $this->setting_model->item('max_record');
    $total = $this->model->count();
    $this->load->helper('paging');
    $paging = paging('admin/'.$this->class . '/index', $total, $max, $offset);
    $this->load->view('admin/' . $this->class . '/list', array_merge($this->_list_data(), array(
                'objects' => $this->model->get_all(null, null, null, null, null, 
                        null, null, null, null, null, null, null,null, $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging)));
  }
  
  public function show($id = '') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $this->load->view('admin/' . $this->class . '/show', array('obj' => $obj));
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/'.$this->class);
  }

  public function create() {
    $this->load->view('admin/' . $this->class . '/create');
  }

  public function save() {
    $id = $this->model->save($_REQUEST);
    if (is_array($id))
      set_flash_error($id);
    else
    if (!empty($id)) {
      set_flash_message($this->lang->line('message.created', 
              array($this->lang->line('label.' . $this->class), $id)));
      redirect('admin/'.$this->class);
      return;
    } else
      set_flash_error($this->lang->line('error.faltal.error'));
    $this->load->view('admin/' . $this->class . '/create', array('obj' => $_REQUEST));
  }

  public function edit($id = '') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $this->load->view('admin/' . $this->class . '/edit', array('obj' => $obj));
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/'.$this->class);
  }

  public function update() {
    $id = $this->model->save($_REQUEST);
    if (is_array($id))
      set_flash_error($id);
    else
    if ($id === true) {
      set_flash_message($this->lang->line('message.updated', 
              array($this->lang->line('label.' . $this->class), $_REQUEST['id'])));
      redirect('admin/'.$this->class);
      return;
    } else
      set_flash_error($this->lang->line('error.faltal.error'));
    $this->load->view('admin/' . $this->class . '/edit', array('obj' => $_REQUEST));
  }

  public function delete($id='') {
    if (!empty($id)) {
      $obj = $this->model->get($id);
      if (!empty($obj)) {
        $this->model->delete($id);
        set_flash_message($this->lang->line('message.deleted', array($this->lang->line('label.' . $this->class), $id)));
        redirect('admin/'.$this->class);
        return;
      }
    }
    set_flash_error($this->lang->line('error.not.found', array($this->lang->line('label.' . $this->class), $id)));
    redirect('admin/'.$this->class);
  }

  public function action() {
    if (!empty($_REQUEST['btnAction']) && !empty($_REQUEST['action_to'])) {
      $action_to = $_REQUEST['action_to'];
      $action_to = is_array($action_to) ? $action_to : array($action_to);
      if ($_REQUEST['btnAction'] == 'delete')
        foreach ($action_to as $id)
          $this->model->delete(array('id' => $id));
      set_flash_message($this->lang->line('message.action.success'));
    } else
      set_flash_error($this->lang->line('error.action.no.select'));
    redirect('admin/'.$this->class);
  }

  public function search($max = '', $offset = '') {
    $offset = $offset + 0;
    $conditions = $_REQUEST;
    $in_list = array();
    $params = array();
    foreach ($conditions as $field => $value) {
      if($value == 'all'){
        unset($conditions[$field]);
        continue;
      }
      if (is_array($value)) {
        foreach ($value as $v)
          $params[] = $field . '[]=' . $v;
        $in_list[$field] = $value;
        unset($conditions[$field]);
      } else
        $params[] = $field . '=' . $value;
    }
    $params = '?'.implode('&', $params);
    $total = $this->model->count($conditions, null, $in_list);
    $max = $this->setting_model->item('max_record');
    $this->load->helper('paging');
    $paging = paging('admin/'.$this->class . '/search', $total, $max, $offset, array(), $params);
    
    $this->load->view('admin/' . $this->class . '/list', array_merge($_REQUEST, $this->_list_data(), array(
                'objects' => $this->model->get_all($conditions, null, 
                        $in_list, null, null, null, null, null, null, null, null, null, null, $max, $offset),
                'total' => $total, 'offset' => $offset, 'max' => $max, 'paging' => $paging )));
  }

}